function ajaxCertus(url, param, successFunc) {
    var access_token = getCookie("access_token");
    var token_type = getCookie("token_type");
    var baseUrl = getCookie("baseUrl");
    $.ajax({
        url: baseUrl + url + "?access_token=" + access_token,
        type: 'POST',
        crossDomain: true,
        cache: false,
        data: JSON.stringify(param),
        processData: false,
        contentType: "application/json",
        dataType: "json",
        headers: {
            'Authorization': token_type + " " + access_token
        }
    }).done(successFunc).fail(function (res, a, b, c) {
    });
}

function findBacklogPersonHead(a, b) {
    a.ajax("headNextassessingOfficer", {templateCode: b}, function (res) {
        nextApprovalManView(res.data);
    })
}

function findBacklogPersonHead1(a, b) {
    a.ajax("headNextassessingOfficer", {templateCode: b}, function (res) {
        nextApprovalManView(res.data);
    })
}

function findBacklogPerson(a, b) {
    a.ajax("nextassessingOfficer", {stepInstanceId: b}, function (res) {
        if (res.data) {
            nextApprovalManView(res.data);
        }
    })
}

function findBacklogPerson1(a, b) {
    a.ajax("nextassessingOfficer", {stepInstanceId: b}, function (res) {
        if (res.data) {
            nextApprovalManView(res.data);
        }
    })
}


function nextApprovalManView(content) {

    // $$scope.modalContent = data.data;

    ;
    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    }
    var modalContent = content;

    $backlogPersonList.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}
var projMoney;
function projProportion(content,triArray,CertusJsonAjax,codeNow) {

    $$scope.ok = function () {
        $modalInstance.close(); // 点击确认后关闭弹窗
    };
    $$scope.cancel = function () {
        $modalInstance.dismiss('cancel'); // 点击取消后撤销模态框
    };

    projMoney = content.$watch("triPercent.money", function (newValue, oldValue, scope) {

        if(newValue){
            surplusMet(triArray,codeNow,newValue,CertusJsonAjax,content);
        }else{
            surplusMet(triArray,codeNow,0,CertusJsonAjax,content);
        }
    });

    var modalContent = content;

    $projProportion.openConfirmWindow("提示", modalContent).then(function (res) {
    });
}

function InitCertusJqNumLimit() {
    var inputs = document.getElementsByTagName("input");
    if (inputs && inputs.length > 0) {
        for (var i = 0; i < inputs.length; i++) {
            var limit = $(inputs[i]).attr("CertusJqNumLimit");
            // var onchange = $(inputs[i]).attr("onchange");
            if (limit) {
                if (limit % 1 === 0) {
                    var limitNum = parseInt(limit);
                    $(inputs[i]).bind('input propertychange', function () {
                        var val = $(this).val();
                        if (val && val != null) {
                            var length = val.length;
                            if (length > 0 && length > limitNum) {
                                layer.msg("字数限制 " + limitNum)
                                $(this).val(val.substring(0, limitNum))
                            }
                        }
                    })
                }
            }
        }
    }
    var areas = document.getElementsByTagName("textarea");
    if (areas && areas.length > 0) {
        for (var j = 0; j < areas.length; j++) {
            var limit = $(areas[j]).attr("CertusJqNumLimit");
            // var onchange = $(areas[i]).attr("onchange");
            if (limit) {
                if (limit % 1 === 0) {
                    var limitNum = parseInt(limit);
                    $(areas[j]).bind('input propertychange', function () {
                        var val = $(this).val();
                        if (val && val != null) {
                            var length = val.length;
                            if (length > 0 && length > limitNum) {
                                layer.msg("字数限制 " + limitNum);
                                $(this).val(val.substring(0, limitNum))
                            }
                        }
                    })
                }
            }
        }
    }
}

function certusValidateNotnull() {
    var $need = $(".needCertus");
    var validate = false;
    if ($need.length) {
        var length = $need.length;
        for (var need = 0; need < length; need++) {
            var $need2 = $($need[need]);
            if (!$($need2).is(":hidden")) {

                if ($($need2).is(":file")) {
                    if (!$need2 || !$need2.val()) {
                        $($need2).attr('style', 'border-color:#b94a48');
                        $($need2).parent().parent().find(".redAlarm").remove();
                        $($need2).parent().parent().append('<p class=\"redAlarm\" style="display: block;margin:5px 0 0 0;color: #b94a48;">* 此为必填项</p>');
                        return false;
                    }
                }else{
                    $($need2).val($($need2).val().replace(/^\s+/, '').replace(/\s+$/, ''));
                    if (!$need2 || !$need2.val() || $need2.val().indexOf("undefined") > 0 || $need2.val() == "" || $need2.val().toUpperCase().indexOf("XXXX") > -1) {

                        // $($need2).attr("style","border-color:red");
                        setTimeout(function () {
                            $($need2).parent().parent().find(".redAlarm").remove();
                            $($need2).attr('style', '');
                        }, 2500);
                        var cname = $($need2).attr("certusName");
                        if (cname == undefined || cname == "" || cname == null) {

                            layer.msg("必填项不能为空");
                        } else {
                            layer.msg(cname + "不能为空");
                        }
                        $($need2).attr('style', 'border-color:#b94a48');
                        $($need2).parent().parent().find(".redAlarm").remove();
                        $($need2).parent().append('<p class=\"redAlarm\" style="display: block;margin:5px 0 0 0;color: #b94a48;">* 此为必填项</p>');
                        return false;
                    }
                }

            }
        }
    }
    return true;
}

function certusValidateNotnullPolyfill() {

    var $need = $(".needCertusPF");
    var validate = false;
    if ($need.length) {
        var length = $need.length;
        for (var need = 0; need < length; need++) {
            var $need2 = $($need[need]);

            if (!$($need2).is(":hidden")) {
                if ($($need2).is(":file")) {
                    if (!$need2 || !$need2.val()) {
                        $($need2).attr('style', 'border-color:#b94a48');
                        $($need2).parent().parent().find(".redAlarm").remove();
                        $($need2).parent().parent().append('<p class=\"redAlarm\" style="display: block;margin:5px 0 0 0;color: #b94a48;">* 此为必填项</p>');
                        return false;
                    }
                } else {
                    $($need2).val($($need2).val().replace(/^\s+/, '').replace(/\s+$/, ''));
                    if (!$need2 || !$need2.val() || $need2.val().indexOf("undefined") > 0 || $need2.val() == "" || $need2.val().toUpperCase().indexOf("XXXX") > -1) {
                        setTimeout(function () {
                            $($need2).parent().parent().find(".redAlarm").remove();
                            $($need2).attr('style', '');
                        }, 2500);
                        if ($($need2).attr("certusName")) {
                            layer.msg($($need2).attr("certusName") + "不能为空");
                        } else {
                            layer.msg("请填入必填项");
                        }
                        $($need2).attr('style', 'border-color:#b94a48');
                        $($need2).parent().parent().find(".redAlarm").remove();
                        $($need2).parent().parent().append('<p class=\"redAlarm\" style="display: block;margin:5px 0 0 0;color: #b94a48;">* 此为必填项</p>');
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

function startMenu(CertusFormAjax, $scope, CertusJsonAjax, $location, $http) {
    var originUrl = $location.$$absUrl.substring($location.$$absUrl.lastIndexOf("#") + 2);
    if (!originUrl || originUrl == "") {
        originUrl = "index"
    }
    CertusJsonAjax.ajax("originUrl", {"originUrl": originUrl}, function (response) {
        $("#userName").html(response.data.userName);
    });
}
//设置cookie
function setCookie(name, value, day) {
    var date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires=' + date;
};

function isLogin(access_token) {

    if (access_token == null || access_token == "logout") {
        return false;
    } else {
        return true;
    }
}
//当点击左侧菜单时，清除临时数据库
$("#sidebar").find("ul[class='sub']").find("a").bind('click',function () {
    localStorage.clear();
});